package org.lzz.controller;

import org.lzz.pojo.Article;
import org.lzz.pojo.PageBean;
import org.lzz.pojo.Result;
import org.lzz.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/article")
// 2025-4-23记：因在apifox中请求路径写成/artical导致报错，查了一个小时bug啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    // 新增文章
    @PostMapping()
    public Result add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success();
    }

    // 分页查询
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ){
        PageBean<Article> ls = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(ls);
    }

    // 文章详情
    @GetMapping("/detail")
    public Result<Article> detail(Integer id){
        Article article = articleService.findById(id);
        return Result.success(article);
    }

    // 更新文章
    @PutMapping
    public Result update(@RequestBody @Validated Article article){
        articleService.update(article);
        return Result.success();
    }

    // 删除文章
    @DeleteMapping
    public Result delete(Integer id){
        articleService.delete(id);
        return Result.success();
    }
}
